﻿using FTBS.Models.BLO;
using FTBS.Models.Entities;
using FTBS.Models.Entities.Concert;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace FTBS.Controllers
{
    public class TicketCartController : Controller
    {
        private TicketCartBLO _ticketCartBLO = new TicketCartBLO();

        public ActionResult Index()
        {
            List<TicketCartConcertEntity> _listTicketCart = new List<TicketCartConcertEntity>();
            _listTicketCart = _ticketCartBLO.GetAllTicketCart();
            return View(_listTicketCart);
        }

        public ActionResult PaymentDetail(string id)
        {
            PaymentConcertEntity payment = new PaymentConcertEntity();
            payment = _ticketCartBLO.GetPaymentByCartId(id);
            //ViewBag.TicketCartId = id;
            return View(payment);
        }

        public ActionResult Create(string id)
        {
            PaymentConcertEntity payment = new PaymentConcertEntity();
            payment = _ticketCartBLO.GetPaymentByCartId(id);
            //TicketCart ticketCart = new TicketCart();
            //ticketCart = _ticketCartBO.GetTicketCartByCartId(id);
            ////ViewBag.TicketCartInfor = ticketCart;
            //if (payment.PaymentId == 0)
            //{
            //    payment.CartId = ticketCart.CartId;
            //    payment.ChargeMoney = ticketCart.TotalPrice;
            //    payment.IsComplete = false;
            //}
            return View(payment);
        }

        [HttpPost]
        public ActionResult Create(Payment payment)
        {
            if (ModelState.IsValid)
            {
                _ticketCartBLO.CreatePayment(payment);
                payment = _ticketCartBLO.GetPaymentByCartId(payment.CartId);
                return View("PaymentDetail", payment);
            }
            return View(payment);
        }

        public ActionResult PaymentInfo(string id)
        {
            PaymentConcertEntity payment = new PaymentConcertEntity();
            payment = _ticketCartBLO.GetPaymentByCartId(id);
            //TicketCart ticketCart = new TicketCart();
            //ticketCart = _ticketCartBO.GetTicketCartByCartId(id);
            ////ViewBag.TicketCartInfor = ticketCart;
            //if (payment.PaymentId == 0)
            //{
            //    payment.CartId = ticketCart.CartId;
            //    payment.ChargeMoney = ticketCart.TotalPrice;
            //    payment.IsComplete = false;
            //}
            return View(payment);
        }

        [HttpPost]
        public ActionResult PaymentInfo(PaymentConcertEntity payment)
        {
            if (ModelState.IsValid)
            {
                int resultUpdate = _ticketCartBLO.UpdatePayment(payment);
                if (resultUpdate == 1)
                {
                    payment = _ticketCartBLO.GetPaymentByCartId(payment.CartId);
                    return View("PaymentDetail", payment);
                }
                else
                {
                    return View("PaymentDetail", payment);
                }
            }
            return View(payment);
        }

        /// <summary>
        /// Delete a payment
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <author>hungph</author>
        /// <date>3/3/2014</date>
        [HttpDelete, ActionName("Delete")]
        public ActionResult Delete(int id = 0)
        {
            Payment payment = _ticketCartBLO.GetPaymentById(id);
            if (payment == null)
            {
                return HttpNotFound();
            }
            _ticketCartBLO.DeletePayment(payment);
            return RedirectToAction("Index");
        }
    }
}
